import qs from "qs"
import envUtil from "@/utils/envUtil"

const devHost = 'https://www.sx2b2c.com'
const apiHost = 'https://www.sx2b2c.com' // 接口地址
const webHost = 'https://h5.sx2b2c.com/sxhjk/stc/#/' // 网址
// const webHost = 'https://yanshi.sx2b2c.com/sxhjk/stc/#/'
// 微信公共平台的appid
const wxCommonPlatformAppId = 'wx56cb827300659f82';


const post = (url, params, callBack, errorCallBack) => {
	let requestUrl = apiHost + url;
	if (envUtil.isDev()) {
		requestUrl = devHost + url;
	}
	uni.request({
		url: requestUrl,
		data: qs.stringify(params),
		method: 'POST',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
			'token': getToken()
		},
		success: ({
			data
		}) => {
			console.log('post1', data)
			if (data.code == 0) {
				console.log('post2', data)
				uni.hideToast()
				uni.hideLoading()

				uni.showToast({
					title: data.msg,
					duration: 3000,
					icon: "none"
				})
				if (errorCallBack) {
					errorCallBack(data)
				}
				return;
			} else if (data.code == -1) {
				uni.hideToast()
				uni.hideLoading()
				uni.showToast({
					icon: "error",
					title: '登录失效，请重新登录'
				})
				clearToken()
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/index/index'
					})
				}, 2000)
				return;
			}
			callBack(data);
		},
		fail: (err) => {
			console.log(url + ' post fail', err);
			let data = err.data
			if (data.code == 401) {
				uni.clearStorage()
			}
			if (errorCallBack) {
				errorCallBack(err)
			}

		}
	})
}

const postReturnSource = (url, params, callBack, errorCallBack) => {
	let requestUrl = apiHost + url;
	if (envUtil.isDev()) {
		requestUrl = devHost + url;
	}
	uni.request({
		url: requestUrl,
		data: params,
		method: 'POST',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
			'token': getToken()
		},
		success: ({
			data
		}) => {
			callBack(data);
		},
		fail: (err) => {
			console.log(url + ' postReturnSource fail', err);
			let data = err.data
			if (data.code == 401) {
				uni.clearStorage()
			}
		}
	})
}

const get = (url, params, callBack, errorCallBack) => {
	let requestUrl = apiHost + url;
	if (envUtil.isDev()) {
		requestUrl = devHost + url;
	}
	uni.request({
		url: requestUrl,
		data: params,
		method: 'GET',
		header: {
			'token': getToken()
		},
		success: ({
			data
		}) => {
			if (data.code == 0) {
				uni.hideToast()
				uni.hideLoading()
				uni.showToast({
					title: data.msg,
					duration: 3000,
					icon: "none"
				})
				if (errorCallBack) {
					errorCallBack(data)
				}
				return;
			} else if (data.code == -1) {
				uni.hideToast()
				uni.hideLoading()
				uni.showToast({
					icon: "error",
					title: '登录失效，请重新登录'
				})
				clearToken()
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/index/index'
					})
				}, 2000)
				return;
			}
			callBack(data);
		},
		fail: (err) => {
			console.log(url + 'get fail', err);
			let data = err.data
			if (data.code == 401) {
				uni.clearStorage()
			}
		}
	})
}



const getToken = () => {
	let token = uni.getStorageSync('token')
	// // todo 测试使用
	// if (!token) {
	// 	token = '6174fff3-43cb-4bc2-94be-16037dad0b23'
	// }
	return token;
}
const setToken = (token) => {
	uni.setStorageSync('token', token)
}
const clearToken = () => {
	uni.removeStorageSync('token')
}

const getA = () => {
	let a = uni.getStorageSync('a')
	if (!a) {
		a = '2fdbcfe31b030ecf259da62de1c0ba8a'
	}
	return a;
}
const setA = (a) => {
	uni.setStorageSync('a', a)
}

export default {
	devHost,
	apiHost,
	webHost,
	wxCommonPlatformAppId,
	post,
	postReturnSource,
	get,
	getToken,
	setToken,
	clearToken,
	getA,
	setA,
}